#!/usr/bin/evn python2.7
# _*_ coding: UTF-8 _*_
import random
import types


def str_enc(s, encoding='utf-8', strings_only=False, errors='strict'):
    if strings_only and isinstance(s, (types.NoneType, int)):
        return s
    if not isinstance(s, basestring):
        try:
            return str(s)
        except UnicodeEncodeError:
            if isinstance(s, Exception):
                return ' '.join([str_enc(arg, encoding, strings_only,
                                         errors) for arg in s])
            return unicode(s).encode(encoding, errors)
    elif isinstance(s, unicode):
        return s.encode(encoding, errors)
    elif s and encoding != 'utf-8':
        return s.decode('utf-8', errors).encode(encoding, errors)
    else:
        return s


def str_random(length, base="0123456789"):
    rstr = []

    for i in range(0, length):
        char = base[random.randint(0, len(base) - 1)]
        rstr.append(char)

    return ''.join(rstr)
